var util = require("util");

module.exports = {
  list: function (req, res, next) {
    console.debug("list action is done");
    console.debug("参数："+util.inspect(req.body));
    var StudentModel = req.models.student;
    var name = req.body.name;
    var age = req.body.age;
    var startNumber = req.body.startNumber;
    var pageSize = req.body.pageSize;
    var condition = {};
    if(name && name !=''){
        condition['name'] = name;
    }else{
      name = '';
    }
    if(age && age !='' && !isNaN(parseInt(age))){
        condition['age'] = parseInt(age);
    }else{
      age = '';
    }
    if( !startNumber || isNaN(startNumber)){
      startNumber = 0;
    }
    if( !pageSize || isNaN(pageSize)){
      pageSize = 10;
    }
    console.debug("name:"+name+",age:"+age+",startNumber:"+startNumber+",pageSize:"+pageSize+",condition:"+util.inspect(condition));
    StudentModel.find(condition).offset(startNumber).limit(pageSize)
          .orderRaw("?? DESC", ['id']).all((err,students)=>{
            if(err){
              console.error("查询学生列表失败，原因："+err.stack);
              res.send({code:"01",msg:"查询列表失败"});
              return;
            }
            var result = students.map( (s)=>{ return s.serialize() ;});

            res.render("student/listView",{name:name,age:age,students:result});
            // res.send({code:"00",name:name,age:age,students:result});
            console.debug("返回结果："+util.inspect(result));

          });
  },
  create: function (req, res, next) {

    var StudentModel = req.models.student;
    var name = req.body.name;
    var age = req.body.age;
    StudentModel.create({name:name,age:age},(err,student)=>{
        if(err){
          console.error("创建学生失败");
          res.send({code:"01",msg:"创建学生失败"});
          return;
        }
        res.render("student/addView");
    });
  },
  delete: function (req, res, next) {
      var StudentModel = req.models.student;
      var id = req.body.id;
      id = parseInt(id);
      StudentModel.get(id,(err,Student)=>{
          if(err){
            console.error("删除学生失败");
            res.send({code:"01",msg:"删除学生失败"});
            return;
          }
          Student.remove((err)=>{
            if(err){
              console.error("删除学生失败");
              res.send({code:"01",msg:"删除学生失败"});
              return;
            }else{
              res.send({code:"00",msg:"删除学生成功"});
            }
          })
      });
  },

  update:function (req, res, next){
      console.log("请求参数："+util.inspect(req.body));
      var StudentModel = req.models.student;
      var id = req.body.id;
      var name = req.body.name;
      var age = req.body.age;
      id = parseInt(id);
      StudentModel.get(id,(err,Student)=>{
           if(err){
            console.error("更新学生失败"+err.stack);
            res.send({code:"01",msg:"更新学生失败"});
            return;
          }
          Student.save({name:name,age:age},(err)=>{
              if(err){
                console.error("更新学生失败");
                res.send({code:"01",msg:"更新学生失败"});
                return;
              }else{
                res.send({code:"00",msg:"更新学生成功"});
              }
          });
      });
    },
    showAddView:function(req, res, next){
      res.render("student/addView");
    },
    showUpdateView:function(req, res, next){
      var id = req.body.id;
      var StudentModel = req.models.student;
      StudentModel.get(id,(err,student)=>{
           if(err){
            console.error("删除学生失败");
            res.send({code:"01",msg:"删除学生失败"});
            return;
          }
          res.render("student/updateView",{name:student.name,age:student.age,id:student.id});
      });
    }

};